<?php
/**
 * Created by PhpStorm.
 * User: Nicof
 * Date: 2016/1/21
 * Time: 16:48
 */

namespace Admin\Controller;
use User\Api\UserApi as UserApi;


class PurchaseController extends AdminController{
    public function confirmpurchase(){
        $map['status'] = 3;
        $res = M('Purchase_list')->where($map)->order('createdAt')->select();
        $this->assign("_list",$res);
        $this->display();
    }


    public function index(){
        $this->display();
    }

    public function purchaseapply(){
        $proName       =   I('proName');
        $status = I('status');
//        $map['status']  =   array('IN',[1010,1011,1012,1013]);   //显示出所有状态小于4的订单，表示财务未审核的
        if(is_numeric($proName)){
            $map['proName']=   array(intval($proName),array('like','%'.$proName.'%'),'_multi'=>true);
        }else{
            $map['proName']    =   array('like', '%'.(string)$proName.'%');
        }
        if ($status) {
            $map['status']=$status;
        }
        $this->meta_title = '采购申请';
        $Product = M('Login') ;

        //$product = $Product->where($map);
        $list = $this->lists($Product,$map);
        $list = int_to_string($list);
        $this->assign('_status', $status);
        $this->assign('_list',$list);
        $this->display('purchaseapply');
    }
    public function createpurchase(){
        if ( empty($this->purchase) ) {
            $this->assign('purchase',array('proName'=>null,'id'=>null,'spec'=>null));//排除notice信息
        }
        $this->meta_title = '创建申请';
        $this->display('addApply');
    }
    public function editpurchase(){
        $purchase = M('purchase')->find( (int)$_GET['id'] );
        $this->assign('purchase',$purchase);
        $this->meta_title = '编辑申请';
        $this->display('addapply');
    }
    public function writepurchase(){
        $purchase = M('purchase');
        $data = $purchase->create();
        if($data){
            $data['status'] = 1010 ;
            if(empty($data['id'])){
                $data['createdAt'] = NOW_TIME ;
                $data['createdUser'] = UID ;
                $data['updatedAt'] = NOW_TIME ;
                $data['updatedUser'] = UID ;
                $r = $purchase->add($data);
            }else{
                $data['updatedAt'] = NOW_TIME ;
                $data['updatedUser'] = UID ;
                $r = $purchase->save($data);
            }
            if($r===false){
                $this->error('操作失败'.$purchase->getError());
            } else{
                $this->success('操作成功!',U('purchaseapply'));
            }
        }else{
            $this->error('操作失败'.$purchase->getError());
        }
    }
    public function purchaselists1(){
        $proName       =   I('proName');
        $map['status']  =   array('IN',[1010,1011,1012,1013,1014]);   //显示出所有状态小于4的订单，表示财务未审核的
        if(is_numeric($proName)){
            $map['proName']=   array(intval($proName),array('like','%'.$proName.'%'),'_multi'=>true);
        }else{
            $map['proName']    =   array('like', '%'.(string)$proName.'%');
        }
        $this->meta_title = '采购列表';
        $Product = M('Login') ;

        $product = $Product->where($map);
        $list = $this->lists($product);
        $list = int_to_string($list);

        $this->assign('_list',$list);
        $this->display();
    }
    public function purchaseLists(){
        $proName       =   I('proName');
        $createdAt = $_GET['createdAt'];

        $time = strtotime($createdAt.'00:00:00');
        $newtime = strtotime($createdAt.'23:59:00');
        $map['createdAt'] =array(between,array($time,$newtime));
        if(is_numeric($proName)){
            $map['proName']=   array(intval($proName),array('like','%'.$proName.'%'),'_multi'=>true);
        }else{
            $map['proName']    =   array('like', '%'.(string)$proName.'%');
        }
        $this->meta_title = '采购列表详情';
        $Product = M('Purchaselist') ;

        //$product = $Product->where($map);
        $list = $this->lists($Product,$map);
        $list = int_to_string($list);

        $this->assign('_list',$list);
        $this->display('purchaselists');
    }
    public function purchaseList(){
        $Model = M();
        $list = $Model -> query("select date_format(from_unixtime(createdAt),'%Y-%m-%d') createdAt,sum(number) number,sum(amount) amount,case when status != 1013 then 1011 else 1013 end status from gr_purchaselist a group by date_format(from_unixtime(createdAt),'%Y-%m-%d')");

        $list = int_to_string($list);
        $this->assign('_list',$list);
        $this->display('list');
    }
    public function details($id = null) {
        $map['id'] = $id;
        $list = M('Purchaselist')->where($map)->select();
        //int_to_string_order($orderlist);
        $purchase = $list[0];

        $this->purchase = $purchase;

        $this->display();
    }

    public function prodetail(){
        $map['batch'] = I('batch');
        $goods=M('purchase')->where($map)->select();
        $this->assign('_list',$goods);
        $this->display();
    }

    public function proaccept(){
        $map['batch'] = I('batch');
        $data['status'] = 4;
        $re=M('purchase_list')->where($map)->save($data);
        if($re){
            $this->success('确认成功',U('confirmpurchase'));
        }else{
            $this->error('确认失败',U('confirmpurchase'));
        }
    }

    public function history(){
        $list = M('Purchase_list')->order("createdAt desc")->select();
        $this->assign("_list",$list);
        $this->display();
    }

    public function historylist(){
        $map['batch'] = I('batch');
        $list=M('purchase')->where($map)->select();
        $this->assign('_list',$list);
        $this->display();
    }
}